package com.example.orderservice_1.service;

import com.example.orderservice_1.domain.Order;
import com.example.orderservice_1.mapper.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderService {

    @Autowired
    private OrderRepository orderRepository;

    // 创建订单
    public Order createOrder(Order order) {
        order.setCreatedAt(new Date());
        order.setStatus("CREATED");
        return orderRepository.save(order);
    }

    // 更新订单
    public Order updateOrder(Long id, Order orderDetails) {
        Order order = orderRepository.findById(id).orElseThrow(() -> new RuntimeException("Order not found"));
        order.setStatus(orderDetails.getStatus());
        order.setUpdatedAt(new Date());
        return orderRepository.save(order);
    }
}
